new function to write csv