Lots of little fixes thanks to Coverity Scan