Factorization of code for pagination