refactored using defaultdict