Updated getNext method