Added unit tests, refactoring.