Started rewriting matching algorithm