refactor code using csv functions