Consolidate changes into single function

Based on suggestions from Tschier to use max_depth() function