Modified to get the best fit sequence