Fix prev next logic and corresponding test