refactored - initial tests pass