Refactor mouse/touch event handling