Added maxdepth and tidied code