Fix bugs detected by Scrutinizer