Pagination code refactor