Refactored logic of show/hide submenu.