Fixes issues that were detected with Scrutinizer

This is the first round of fixing the code based on the scrutinizer
report.

Fixes: #4