More refactoring; mostly complete now