Reorganization and small improvements of the main branch. No logic was changed; just a bit of structure in the source code.