Refactored tree generation code, added toggle button for popover