Fixes for bugs discovered by Coverity Scan and myself