Fixed prev/next behavior. JS code rafactoring.