Problem 1:
it seems you have used MODAL windows. Modal Windows do not return control to main application until they are closed.
You need to have modeless windows each with its window message plus a a global message pump

Problem 2:
not sure, maybe you are passing over the quit command from last window to main window.
Also you need to keep track how many windows are still there and close only when needed.

I still do not understand under which programming language are you developing