Extract handleEvents handler