Bug fixes, working code