Refactoring and unit tests