Misc changes

This is a resubmission of an old pull request after fixing some issues.  Individual commit messages explain the changes.