Added write_to_CSV helper function