Changed to a more secure way of handling tempfiles