Fixes for various bugs noticed by Coverity static analysis tool.