Working flatten tree code added