Misc

Miscellaneous fixes and refactorings.
(some older local branches attempted cleanup!)