Applied all the fixes suggested by @xabbuh