refactor output to csv