Further improvements on formatting function