rewrite of commutate function so it is easier to maintain