Some improvements thanks to dbuenzli's review