improved logic to choose right window to show dialogs in