Replaced some usages of dict.setdefault with a defaultdict.

This should be completely backwards-compatible since it only
changes internal variables that are not exported outside of
their respective functions.