More reorganization, cleanups, and unit tests.