More refactoring; tests passing