first part of the fix for #88 Need to handle closing window + keep the correct focus.