Added bounds checks to PREV and NEXT