Updated changes to produce csvs by flushing and closing