Open/close docked window for certain main windows

I have a docked window that is only for certain main windows (Main Window 1,2, and 3). On startup, Vision Client opens up to Main Window 1 with the docked window. I want to close the docked window when the user opens a different window of Vision Client, or none of the main windows of interest are open. However, even when another main window is open, the docked window remains. Therefore, I added this component scripting under the docked window's visionWindowOpened.

main_window_names = ["Main Window 1",
					"Main Window 2",
					"Main Windows 3",

def are_main_windows_open():
	for window_name in main_window_names:
		window = system.gui.getWindow(window_name)
		if window is None:
			return False
	return True
def close_docked_if_needed():
	docked_window = system.gui.getWindow("Docked Window")
	if docked_window is not None:
		if not are_main_windows_open():


However, I'm getting a ValueError that "Main Window 2 is not currently open." I'm not sure if my code is incorrect or if I shouldn't script this under the docked window's event handler.