cleaned some duplicated code for pagination