Refactoring. Added unit test coverage.