implemented tiebreak logic (but ugly)