Use defaultdict to simplify code.