Fix Next impl

Needs some refactoring and some optimization, but fixes several bugs.