Assorted cleanup

This is a smorgasbord of little cleanups. I recommend looking at them one commit at a time, instead of as a single big diff.

The public API does not change as part of this pull request.