Refactor pagination