Major refactoring and clean up, in preparation for release to the real world.