Fixed some matching bugs; made the logic of the matching code a little