Updated Code to handle subtree dynamically.