catch both exit and close events