Fix to catesian to ternary transformation