Fixed bugs. Added more tests.