extended version of function next