Various fixes (many thanks to Gsuz)