Updated the matching algorithm to allow for different orders