Major reorganization to cleanup code layout

Functionally the same, just move things around so that adding new stuff
will be more organized.