Added a function to count the number of pages.