modified max_depth code to not go into infinite loop