Yet more code reorganization, hopefully making things cleaner.