Refactoring stage one passes tests