Added functionality to write to csv.