format_result function extracted