extract the rule matching code into a individual function