second pass commands refactor, bug fixes
don't modify collections while enumerating them