Refactored code and added tests.