private List<List<Gate>> getSortedGates(List<Gate> gates) {
		List<List<Gate>> res = new ArrayList<List<Gate>>();
		GateTypes current = null;
		int index = 0;
		Gate trial = gates.get(0);

		if (trial.isXOR()) {
			current = GateTypes.XOR;
		} else if (trial.isAND()) {
			current = GateTypes.AND;
		} else if (trial.isINV()) {
			current = GateTypes.INV;
		}

		res.add(new ArrayList<Gate>());
		for (Gate g: gates) {
			if (equal(g, current)) {
				res.get(index).add(g);
			} else {
				res.add(new ArrayList<Gate>());
				index++;
				res.get(index).add(g);
				current = getNewCurrent(g);
			}
		}

		for (List<Gate> list: res) {
			Collections.sort(list, new LayerComparator());
		}
		return res;
	}