Multiple fixes

Please see the individual commits for more details.