refactoring and adding unit tests