matching algorithm almost ready, needs only debugging