Break down into files by function.

This is much better than one monolithic code file, but sadly everything is
still tightly coupled through heavy use of globals.