some refactoring and tests passing. needs more cleanup