refactor pattern matching