Fix some bugs found by CoverityScan