Fix various issues recommended by scrutinizer.
Merge branch 'dev-scr-bugs' into dev
Merge.