public static Element extractMathElement(Document document) {
        /* Should only have 1 child of doc root (<body/>) element here, which should be <math/>.
         * We'll make that the new root Node */
        try {
            Node rootElement = document.getChildNodes().item(0);
            
            Element firstMathElement = null;
            NodeList childNodes = rootElement.getChildNodes();
            for (int i=0, length=childNodes.getLength(); i<length; i++) {
                Node childNode = childNodes.item(i);
                if (MathMLUtilities.isMathMLElement(childNode, "math")) {
                    if (firstMathElement!=null) {
                        Assert.fail("Found more than one <math/> children");
                    }
                    firstMathElement = (Element) childNode;
                }
                else if (childNode.getNodeType()==Node.ELEMENT_NODE) {
                    Assert.fail("Found unexpected element under root");
                }
                else if (childNode.getNodeType()==Node.TEXT_NODE && childNode.getNodeValue().matches("\\S")) {
                    Assert.fail("Found non-whitespace text Node");
                }
            }
            if (firstMathElement==null) {
                Assert.fail("No <math/> child found");
            }
            document.removeChild(rootElement);
            document.appendChild(firstMathElement);
            
            return firstMathElement;
        }
        catch (AssertionFailedError e) {
            log.severe("Resulting DOM Document did not have expected structure. Got:\n"
                    + MathMLUtilities.serializeDocument(document));
            throw e;
        }
    }