consolidate tmpfile logic

Also use tempfile so that "/tmp" isn't hard-coded.