implemented a max depth function