added writes to each loop in functions