public function paged_search(\stdClass $formdata, $pagenum) {
        $out = new \stdClass();

        if (self::is_search_area_categories_enabled() && !empty($formdata->cat)) {
            $cat = self::get_search_area_category_by_name($formdata->cat);
            if (empty($formdata->areaids)) {
                $formdata->areaids = array_keys($cat->get_areas());
            } else {
                foreach ($formdata->areaids as $key => $areaid) {
                    if (!key_exists($areaid, $cat->get_areas())) {
                        unset($formdata->areaids[$key]);
                    }
                }
            }
        }

        $perpage = static::DISPLAY_RESULTS_PER_PAGE;

        // Make sure we only allow request up to max page.
        $pagenum = min($pagenum, (static::MAX_RESULTS / $perpage) - 1);

        // Calculate the first and last document number for the current page, 1 based.
        $mindoc = ($pagenum * $perpage) + 1;
        $maxdoc = ($pagenum + 1) * $perpage;

        // Get engine documents, up to max.
        $docs = $this->search($formdata, $maxdoc);

        $resultcount = count($docs);
        if ($resultcount < $maxdoc) {
            // This means it couldn't give us results to max, so the count must be the max.
            $out->totalcount = $resultcount;
        } else {
            // Get the possible count reported by engine, and limit to our max.
            $out->totalcount = $this->engine->get_query_total_count();
            $out->totalcount = min($out->totalcount, static::MAX_RESULTS);
        }

        // Determine the actual page.
        if ($resultcount < $mindoc) {
            // We couldn't get the min docs for this page, so determine what page we can get.
            $out->actualpage = floor(($resultcount - 1) / $perpage);
        } else {
            $out->actualpage = $pagenum;
        }

        // Split the results to only return the page.
        $out->results = array_slice($docs, $out->actualpage * $perpage, $perpage, true);

        return $out;
    }