Condensed the next() function to a single, generic case.