Remove potential race condition

There was a potential for a different file with the same temp name to be created between the generation of the temp filename and it's creation. Fix this by actually creating the file and using mktemp to handle the temporary directory location as well.