private function buildChildNode($node, $references, $maxLevel)
    {
        if (isset($references[$node->getParentId()])) {
            $level = $references[$node->getParentId()]->getLevel() + 1;

            // Check if max level is not reached or not set at all.
            if ($maxLevel == 0 || $level <= $maxLevel) {
                $node->setLevel($level);
                $node->setParent($references[$node->getParentId()]);
                $references[$node->getParentId()]->setChild($node, $node->getId());
            }
        }
    }