Refactored to use a dictionary instead of a set.