added beforeClose event