Refactored code, added tests and got them passing