Add boundry checking for 'next' and 'prev'