Various fixes as discovered with scrutinizer