Refactor using functions and a list comprehension