refactor maxDepth