Fix various problems found by Coverity