Fixed a bug that was not properly choosing the best index