Improved code for tree view popup menus (contributes to #7 and #9)