Misc

Miscellaneous fixes and refactorings.
(some older local branches attempted cleanup!)

Usually minor. A few of the commits in the other PR on attachments were included. (that one can be closed)