updated csv writer function