improved script

Improved advance pagination script and fix bug caused while decrementing
values.