finish refactoring and initial test fixes