Deleting while iterating over a dictionary
Keep a list of the keys you wish to remove as you find them. Then, when you are done, iterate over this list, calling myDictionary.Remove(key)
on each key you stored.
Try using a separate dictionary, and instead of marking the key values to delete, insert the key values that you want to keep, and do this at the end of the iteration:
old_map = new_map