private void collapseTree(JSONArray tree, JSONArray flattened) throws JSONException {
//        List<JSONObject> leaves = new ArrayList<JSONObject>();
//        if (nLeaves > clusterNumLeavesThreshold) {
            if (tree.get(2) instanceof JSONArray) {
                if (((JSONArray) tree.get(2)).getInt(0) == -1) {
                    JSONObject dupe = new JSONObject();
                    dupe.put("id", ((JSONArray)tree.get(2)).getJSONObject(2).get("id"));
                    dupe.put("dupes", ((JSONArray)tree.get(2)).getJSONObject(3).get("dupes"));
                    tree.put(2, dupe);
                } else collapseTree((JSONArray) ((JSONArray) tree).get(2), flattened);
            } 
            if (tree.get(3) instanceof JSONArray) {
                if (((JSONArray) tree.get(3)).getInt(0) == -1) {
                    JSONObject dupe = new JSONObject();
                    dupe.put("id", ((JSONArray)tree.get(3)).getJSONObject(2).get("id"));
                    dupe.put("dupes", ((JSONArray)tree.get(3)).getJSONObject(3).get("dupes"));
                    tree.put(3, dupe);
                } else collapseTree((JSONArray) ((JSONArray) tree).get(3), flattened);
            }
//        } 
//        else {
//            ArrayList<JSONObject> clusterleaves = new ArrayList<JSONObject>();
//            if (tree.get(2) instanceof JSONArray) {
//                getAllLeaves((JSONArray)((JSONArray) tree).get(2), clusterleaves);
//            } else leaves.add((JSONObject)tree.get(2));
//            if (tree.get(3) instanceof JSONArray) {
//                getAllLeaves((JSONArray)((JSONArray) tree).get(3), clusterleaves);
//            } else leaves.add((JSONObject)tree.get(3));
//            JSONArray group = new JSONArray();
//            for (JSONObject jso : clusterleaves) group.put(jso);
//            flattened.put(group);
//        }
    }