Assorted bugfixes

Fixes for a few bugs revealed by building with a different compiler, plus some minor cleanup.