Corrected next/prev function