Corrected and simplified pagination code.