rewrote the match code