Fix some bugs discovered with Coverity