Fixes for bugs discovered by Coverity Scan