Updated code with improvements regarding events.