Updated rule matching, contains bugs, needs more testing