Various fixes

See commits for more details.