added maxDepth limitation