Made prev/next use the same code

Also replaced conditional block with a simpler MOD operator.