added end session detection to save window state