Refactored the code and tests