Completed refactoring, almost ready for integration tests