refactor pagination logic into utility method and add tests. Closes #16