fix problem from modifying dict while iterating over it