partial refactor now passing unit tests