Various fixes to defects found using statical analysis tooling

This is a first part of a work in progress