Improved pagination code.