Work in progress on refactoring to support record merging

All tests still pass.