fix wrong conditional
fix possible iterator invalidation