refactor input pattern matching