refactored the code for the matching to make it more elegant.