Finished recursive matching algorithm