Removed code duplication by using generic CSV writing function