Move mouse and touch event handlers into separate functions.