Added CSV I/O support, refactored a bit