Fix and better CSV reader/writer code