protected function processElements(Plugin $plugin, &$log = '', $xml = false)
    {

        if (!$xml)
            $xml = $this->loadXML($plugin->Path);

        if(!$xml)
            throw new Exception('Missing plugin.xml file for plugin ['.$plugin->Slug.']');

        $processedSlugs = array();

        // add elements
        $elements = $xml->elements;
        if (!empty($elements)) {
            foreach ( $elements->element as $elementNode ) {

                $element = new Element();
                $this->ModelMapper->defaultsOnModel($element);

                $this->SystemXMLConverter->xmlToElement($element, $elementNode);

                $log .= "Processing element descriptor: {$element->Slug}\n";

                if ($this->ElementService->slugExists($element->Slug)) {
                    $existingElement = $this->ElementService->getBySlug($element->Slug);

                    if ($existingElement->PluginID != $plugin->PluginID)
                    {
                        $log .= 'WARNING: Element already exists for slug: '.$existingElement->Slug."\n";
                        continue;
                    }

                    $element->ElementID = $existingElement->ElementID;
                }

                $element->PluginID = $plugin->PluginID;
                $anchorSite = $this->SiteService->getAnchoredSite();
                if(empty($anchorSite))
                    throw new Exception('Cannot install element without anchor site');
                $element->AnchoredSiteSlug = $anchorSite->getSlug();
                $element->AnchoredSiteSlugOverride = $anchorSite->getSlug();
                $element->AnchoredSite = $anchorSite;

                if ($this->ElementService->slugExists($element->Slug)) {
                    $log .= "Element updated...\n";
                    $this->ElementService->edit($element);
                } else {
                    $log .= "Element created...\n";
                    $this->ElementService->add($element);
                }

                $log .= "ID: {$element->ElementID}\n";
                $log .= "Slug: {$element->Slug}\n";
                $log .= "Name: {$element->Name}\n";
                $log .= "Description: {$element->Description}\n";
                $log .= "\n";

                $log .= "Creating element DB schema...";
                $element = $this->ElementService->getBySlug($element->Slug); //refresh schema
                $this->NodeService->createDBSchema($element);
                $log .= "done.\n\n";
            }
        }
    }