protected boolean _read(Node n) { try {
                 Object ret = xml.next();
                 if (ret == null) return false;
-                if (ret instanceof String) {
-                    n.cdata = (String)ret;
+                if (ret instanceof XML.Text) {
+                    n.cdata = ((XML.Text)ret).t;
                     n.delta = xml.getDepth() - currentdelta;
                     currentdelta = xml.getDepth();
                     return true;
             } catch (Exception e) { throw new RuntimeException(e); } }
         }
 
-        public void toXML(Writer writer) throws IOException { Node n = new Node(); if (read(n)) toXML(writer, n); }
+        public void toXML(Writer writer) throws IOException {
+            Node n = new Node();
+            do { if (!read(n)) n = null; } while (n!=null && n.cdata != null);
+            toXML(writer, n);
+        }
         private Node toXML(Writer w, Node n) throws IOException {
             final String name = n.name;
             if (n.cdata != null) {
                     w.write("/>");
                 } else {
                     w.write(">");
-                    while(n != null && n.delta > 0) n = toXML(w, n);
+                    while(n != null && n.delta > 0) {  n = toXML(w, n); }
                     w.write("</");
                     w.write(name);
                     w.write(">");
 
         servletscope = new ServletScope(request, response, cx);
         path = cx.getRealPath(((HttpServletRequest)request).getServletPath());
         Reader xmlreader = new InputStreamReader(new FileInputStream(path));
-        new Template(servletscope, servletscope, xmlreader).wrap(null).toXML(response.getWriter());
+        Writer w = response.getWriter();
+        new Template(servletscope, servletscope, xmlreader).wrap(null).toXML(w);
+        w.flush();
     }
 
     public class ServletScope extends JS.Obj {
 
     }
 
     public static Node transform(Node n, Scope scope) {
-        if (n.cdata != null) n.cdata = eval(n.cdata, scope).toString();
-        else for(int i=1; i<n.numattrs*2; i+=2) n.attrs[i] = eval(n.attrs[i], scope).toString();
-        return n;
+        try {
+            if (n.cdata != null) n.cdata = eval(n.cdata, scope).toString();
+            else for(int i=1; i<n.numattrs*2; i+=2) n.attrs[i] = eval(n.attrs[i], scope).toString();
+            return n;
+        } catch (JSExn e) { throw new RuntimeException(e); }
     }
 
-    private static Object eval(String s, Scope scope) {
+    private static Object eval(String s, Scope scope) throws JSExn {
         if (s == null) return null;
         StringBuffer ret = new StringBuffer();
         for(boolean first = true; s.indexOf("${") != -1; first = false) {
             ret.append(s.substring(0, s.indexOf("${")));
             String s2 = s.substring(s.indexOf("${")+2);
-            Object app = exec("return (" + s2.substring(0, s2.indexOf('}')) + ");\n", scope);
+            JS app = exec("return (" + s2.substring(0, s2.indexOf('}')) + ");\n", scope);
             s = s.substring(s.indexOf('}') + 1);
             //if (first && s.trim().length() == 0) return app;
-            if (!(app == null || app instanceof String || app instanceof Number || app instanceof Boolean))
+            if (!(app == null || app instanceof JSPrimitive))
                 throw new RuntimeException("javascripts within ${...} can only return strings, numbers, and booleans; not a " +
                                            app.getClass().getName());
-            ret.append(app == null ? "null" : app.toString());
+            ret.append(app == null ? "null" : JSU.toString(app));
         }
         ret.append(s);
         return ret.toString();
     }
 
-    public static Object exec(String s, Scope scope) {
+    public static JS exec(String s, Scope scope) {
         try {
             return JSU.cloneWithNewGlobalScope(JSU.fromReader("input", 0, new StringReader(s)), scope).call(null,null);
         } catch (Exception e) {
         }
     }
 
-    public static class Scope extends JS.Immutable {
+    public static class Scope extends JS.Obj {
         private final JS parent;
         private final Hash declared = new Hash();
         public Scope(JS parent) { this.parent = parent; }