FIX: Avoid invalid memory access (possible crash) due to modification of a map that is being iterated over