Method 'countElements' modified to solve problems with pagination