Refactor to lose the temp file, and cleaned up code