improved rule matching