Refactor input handling