Separated the temporay file build logic

Moved to its own method to reduce function complexity.