Refactored CSV reading and writing