Coverity fixes

Fix code that was marked as bad by Coverity.
The corresponding Coverity issues are stated in the commit messages.