Misc cleanup and bugfixes.