Refactored code to look nicer. Tests still pass