Fixed bugs. Refactored code and tests.