complete working version. random pick for ties.