Rewrote edge matching function (incomplete).