Use opencsv to generate CSV output. Refactor. Add tests for edge cases.