Simplified code slightly by using list comprehensions.