Fixed pagination function