added document destroyed event, changed the codes for the events