Fixing array modification issue