refactoring and added unit tests