very messy initial working version of CSV writer