Added iterative writing of results to file