Refactor CSV parsing logic for less code duplication