refactored check for matches code