Added unit tests and refactored.