beforeClosing handler