Refactored code, passing tests