Fix various issues discovered by Coverity