Fix CSV and writer bugs; cleanup