refactor to move temp file open and close to same function