Fix some bugs and scrutinizer warnings