Corrected loop issue, csv is writing