Extracted apply_format method