Format optimal string. Some code cleanup