use direction *=-1 instead of ahead() and back()