Fix bugs found with scrutinizer