Refactor to use CSV module to write csv files