Fix various coverity warnings

This is a patch to fix various coverity warnings.