refactored pattern matching