refactoring on pattern matching