A bunch of reorganization and cleanup, but it doesn't work yet.