extracts event handling logic