Some refactoring; intermediate commit. Compiles, at least.