Code refactoring and added unit tests