Added some error handling
Tree detection and test cases