refactored matching code.