rehauled code to produce for accurate CSV