Added handling of window close and command-line kill