Various fixes and tweaks

See commits for details.