Pagination

- Moving pagination link generation out of templates.
- Fixing API bug where queries were maintained across requests.