Refactor CSV parsing and code cleanup