modify to refactor pattern matching environment