Misc fixes

Bunch of fixes, see commits for details.