update next_or_prev to enable looping