Fix invalid iterator access