Fixes based on Scrutinizer inspections

This fixes a few issues spotted by the Scrutinizer inspections.

You can see the individual commit messages for explanations on each change