restructuring csv code