Coverity fixes part 1

This is a bunch of fixes for issues found by coverity in v3.0.4 rc2 release.

More to come, probably, as I go further through the list.