fixed pagination function