Bugfixes. See commit messages for details.