Refactoring the code and tests