private int wandNext(int upperBound) {
		int pTerm, pivot, aterm;

		while (true) {

			pTerm = findPivotTerm(upperBound);
			if (pTerm < 0)
				return -1;
			pivot = postings.get(pTerm).current();
			if (pivot == -1)
				return -1; // lastID
			if (pivot <= currDoc) {
				aterm = pickTerm(pTerm - 1);
				sumSkips += postings.get(aterm).goTo(currDoc + 1);
				numGotoCalls++;
				if (postings.get(aterm).current() < 0)
					postings.remove(aterm);
				else
					sortPostingsBySwap(aterm);
			} else {
				if (postings.get(0).current() == pivot) {
					currDoc = pivot;
					return currDoc;
				} else {
					aterm = pickTerm(pTerm - 1);
					sumSkips += postings.get(aterm).goTo(pivot);
					numGotoCalls++;
					if (postings.get(aterm).current() < 0)
						postings.remove(aterm);
					else
						sortPostingsBySwap(aterm);
				}
			}

		}
	}