FIX race conditions in tempdir creation

And simplify the code. Unfortunately, this fails some tests for reasons that I don't understand; maybe I misunderstood the meaning of `temp_folder`, but I assumed it would be some path like `/var/tmp` inside which a temp directory can be created?