Fixed bugs and removed test code.