Fixed a bug on detecting the last page.