All of this would go away with using `defaultdict`