refactor max depth