Refactor and make sure that all tests pass