Refactor pagination loop.