fixing infinite cycle of opening and closing events