Miscellaneous cleanup and bugfixing