More refactoring, regression tests are passing again.