Move text formatting to a separate function