Refactor to isolate pagination handling