Fix use of invalidated iterator