Small refactor using itertools