refactored the next() method to hold the invariant that each time prev holds the next position