Optimizing function for generate_paginator