Refactored the code and test