Switching between browser windows (not Tabs) and closing the pop up

I am working on a flow where during the workflow of a browser app a pop-up window may or may not occur and I am in need of closing that window. I have been able to switch to that window and close it but I then loose browser context getting back to the original browser window and looking for suggestions on how to handle. I’ve been through several forum posts and code samples. Below are the relevant screenshots. Unfortunately, this is not something I cannot zip up for others to test with. The script named “Switch back to Main Window” produces the error in the 4th screenshot.

image

image

image

My original flow did not show the Switch to Browser action showing I am working within a browser contect.

image