Fixed bugs and added tests.