Fixed bugs in getNext().