Implement yr_rules_foreach and use it where appropriate