Refactored filtered pagination code.