Added a bunch of fixes suggested by @xabbuh