move most event handlers to a separate function to make it easier to manage