adding and removing events through a function