fixing the issue of events not closing properly