improved focus window logic