Updated to fix a bug in case of a tie. The loop kept going.