added tests and refactored code.