Improved code for calendar close event