Modified treeplyr functions to have better error handling, more flexibility for programmatic use