J.W. added sort and remove duplictes to the two basic transition functions.