Added logic to do export to file inside a thread