We could probably use multiple list comprehensions to make this slightly more readable and remove multiple levels of nested.