A lot of fixes and improvements, see the single commit messages for more information