Fix topIndex bug