Add tree sanity testing and use shared_ptr for Buffer