More refactoring.

Still very much a work in progress.