Re-work pagination and add tests.