Various fixes thanks to Scrutinizer