Various fixes

Thanks to enami for reviewing