A bunch of changes thanks to @xabbuh and @stof