fixing runtime issue where collections are modified during iteration