Fixed bugs and removed debug code
Fixed more bugs