Re-factored to deal with line endings which were causing a problem with OSX Excel produced CSV files