Move tmp directory creation back into loop

The temp directory/directories need to be made in the loop or the files
will not be uniquely named.