Refactoring to new structure (not yet complete, but compiles).