     $ "//triangle", (node) ->
       indices[indiceCount++] = parseInt(read node, "v#{k}") for k in [1..3]
 
+    # Helper Functions
+    isFlat = (face) -> ( (n = face.normals)[0] == n[1] == n[2] )
+    cross = (vA, vB) -> [
+        vA[1]*vB[2] - vA[2]*vB[1],
+        vA[2]*vB[0] - vA[0]*vB[2],
+        vA[0]*vB[1] - vA[1]*vB[0]
+      ]
+    magnitude = (v) -> Math.sqrt Math.pow(v[0],2) + Math.pow(v[1],2) + Math.pow(v[2],2)
+    
+    # Adds a face's vertices and normals to the mesh at the given index
+    addFace = (face, mesh, index) -> for attr in ['vertices', 'normals']
+      mesh[attr][index+j*3+k] = face[attr][j][k] for j in [0..2] for k in [0..2]
+      
+
     # Expanding (duplicating) the normals and verts so that there is a 1:1 of verts to indices
     # This allows us to modify the normals on a per-face basis in edges and makes mesh spliting trivial
-    nOfTriangles = @nOfTriangles = indices.length/3 # TODO: when noftriangles is fixed this will be implicit
+    nOfTriangles = indices.length/3 # TODO: when noftriangles is fixed this will be implicit
     exp = {}
-    exp[attr] = new Float32Array(@nOfTriangles*9) for attr in ['vertices', 'normals']
-    @_eachFace (face, i) ->
-      console.log i
-      for attr in ['vertices', 'normals']
-        exp[attr][i*9+j*3+k] = face[attr][j][k] for j in [0..2] for k in [0..2]
+    exp[attr] = new Float32Array(nOfTriangles*9) for attr in ['vertices', 'normals']
+    @_eachFace (face, i) -> addFace(face, exp, i*9)
     indices[i] = i for i in [0..indices.length-1]
     @[attr] = exp[attr] for attr in ['vertices', 'normals']
     @verts = @vertices
 
     # Define the normals for verts without a normal as the normal vector of the face
+    @_eachFace @_calculateVertexNormals
+
+    #return
+    # Counting Curved Surfaces
+    subdivisionLevels = 1 # 4
+    trianglesPerSurface = Math.pow 4, subdivisionLevels
+    @_eachFace (face) -> nOfTriangles += trianglesPerSurface if !isFlat face
+    console.log nOfTriangles*9
+
+    # Subdividing Curved Surfaces
+    exp[attr] = new Float32Array(nOfTriangles*9) for attr in ['vertices', 'normals']
+    vertCount = 0
+
+    subdivide = (face, fIndex) ->
+      # calculating each edge midpoint and normal
+      midVerts = []
+      midNormals = []
+      for edge in [[0,1], [1,2], [2,0]]
+        v = [ face.vertices[edge[0]], face.vertices[edge[1]] ]
+        n = [ face.normals[edge[0]], face.normals[edge[1]] ]
+        d = ( v[1][i] - v[0][i] for i in [0..2] )
+        t = for i in [0..1]
+          crossProduct = cross cross(n[i], d), n[i]
+          ( magnitude(d) * crossProduct[j] / magnitude(crossProduct) for j in [0..2] )
+        midVerts.push v01 = hermiteSpline(0.5, v, t, fIndex)
+        midNormals.push n01 = [0, 0, 0]# TODO: how to calculate the normal!?
+        #console.log "-----------------------" if fIndex < 2
+        # console.log n01 if fIndex < 2
+
+      newFaces = for i in [0..2] # calculating the outer subdivided faces
+        vertices: [ midVerts[i], midVerts[ j = (i+2)%3 ], face.vertices[i] ]
+        normals: [ midNormals[i], midNormals[j], face.normals[i] ]
+      #newFaces = []
+      newFaces.push # calculating the center subdivided faces
+        vertices: midVerts, normals: midNormals
+
+      # adding each subdivided face to the mesh
+      for f in newFaces
+        addFace f, exp, vertCount
+        #addFace face, exp, vertCount
+        vertCount += 9
+      return newFaces
+
+    # iterating through each original face and subdividing it if it is has non-uniform normals
+    @_eachFace (face, fIndex) ->
+      if isFlat face
+        for attr in ['vertices', 'normals']
+          exp[attr][vertCount+j*3+k] = face[attr][j][k] for j in [0..2] for k in [0..2]
+        vertCount += 9
+      else
+        faces = [face]
+        for i in [0..subdivisionLevels-1]
+          newFaces = []
+          for f in faces
+            newFaces.push f2 for f2 in subdivide f, fIndex + Math.pow(4,i)
+          faces = newFaces
+
+    console.log @vertices.length
+    console.log vertCount
+    console.log indices.length
+    @indices = @indices = new Float32Array(nOfTriangles*3)
+    @indices[i] = i for i in [0..@indices.length-1]
+    @[attr] = exp[attr] for attr in ['vertices', 'normals']
+    @verts = @vertices
+
+    # TODO: temporarily using face normals for the subdivided faces until I figure out how to calculate them.
+    @nOfTriangles = nOfTriangles
     @_eachFace @_calculateVertexNormals
 
     # Parsing Edges (TODO)

    xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
    xmlDoc.async=false
    xml.loadXML(text)

base64_encode = (data) ->
  # http://kevin.vanzonneveld.net

        f.normals[i][j] = vN[j] for j in [0..2]

  # Splits the object into 2^16 vert chunks and returns the chunks
  split: () => # TODO: since each object's verts are 1:1 with indices at this point we can use monolithic subarrays here
    console.log "splitting"
    bytesPerMesh = Math.pow(2,16) # is this even in bytes!?
    bytesPerMesh -= bytesPerMesh % 9 # Rounding the bytes per mesh down to the nearest face

