@SuppressWarnings("unchecked")
	private List<List<Gate>> getLayersOfGates(List<Gate> gates) {
		List<List<Gate>> layersOfGates = new ArrayList<List<Gate>>();
		initMaps(gates);

		int totalNumberOfInputs = circuitParser.getNumberOfInputs();
		/*
		 * Loop to run through each list in our MultiMap, first runs through all
		 * gates with left input 0, 1, 2, ..., #inputs.
		 * For each of these "input" dependant gates, we visit them recursively
		 * and set a timestamp on each of these.
		 */
		for (int i = 0; i < totalNumberOfInputs; i++) {
			Collection<Gate> leftList = leftMap.getCollection(i);
			if(leftList == null){
				continue;
			}
			for (Gate g: leftList) {
				visitGate(g, 0, layersOfGates);
			}
		}

		/*
		 * Now that we've visited all gates which depends on a left input, we
		 * do the same for the right input and recursively visit them again.
		 * When we visit a gate which has already been visited we set a
		 * time stamp again to be the max of the current time and the time stamp
		 * of the gate. This value determines which layer the gate is to be
		 * placed in. 
		 */
		for (int i = 0; i < totalNumberOfInputs; i++) {
			Collection<Gate> rightList = rightMap.getCollection(i);
			if (rightList == null) {
				continue;
			}
			for (Gate g: rightList) {
				layersOfGates = visitGate(g, 0, layersOfGates);
			}
		}
		return layersOfGates;
	}