Refactor input event handling