Minor refactorings and cleanup

Several unrelated minor refactorings and cleanups.

These are best reviewed commit-by-commit.