Added temp file writing for multithreading