factored out generic pagination