Added ability to detect window closing