Refactoring code and tests