refactored Format() function