Refactored code and added basic unit tests.