Misc. bugfixes

These are miscellaneous bugfixes which are too small to fit into a single pull request.