refactor format function