Fixed infinite loop in findBestMatch