refactoring and unit tests