Refactoring complex list comprehension.