More robust code.
Cleanup: {} -> defaultdict.