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.

Also throw "'s around filenames to handle unorthodox characters (i.e. spaces) in them. This should never be a problem but it doesn't hurt.

Note: I haven't tested this thoroughly, but running the edits I made on a command line produced the output I expected.