Move the printing/writing of data out of the loop