Refactoring and added unit test.