CSV generation works. Refactoring, commenting and optimization still to be done.