Fix for SCA and CID issues

Various patches for issues found by SCA and for 5 issues reported by Coverity