Fix miscellaneous things uncovered by static analysis

See commit messages for individual changes.

Built on PR #340.