Carried out optimization of Ternary as suggested by Lionello Lunesu.