private static void traverse(RefElement element, List<List<RefElement>> paths, Integer depth) {
    int lastPathsIndex = paths.size() - 1;
    List<RefElement> path = paths.get(lastPathsIndex);
    if (path.contains(element)) {
      return;
    }
    path.add(element);
    List<RefElement> outRefs = new ArrayList<>(element.getOutReferences());
    for (int i = 0; i < outRefs.size(); i++) {
      RefElement outRef = outRefs.get(i);
      if (i > 0) {
        List<RefElement> pathCopy = new ArrayList<>(path.subList(0, depth + 1));
        paths.add(pathCopy);
      }
      depth++;
      traverse(outRef, paths, depth);
      depth--;
    }
  }
private static void traverse(RefElement element, List<List<RefElement>> paths, Integer depth)