Replace separate functions for pagination into a new more general one