Does it ever backfire?
Its not running too lean is it? Does it surge when its running, either at speed or at idle? If it does, the carburetor is too lean. And a lean mixture will cause the exhaust and muffler to run extra hot.
It could also be a burnt valve or seat, or the exhaust valve has no running clearance when hot. It might pay to adjust the valves.
The reason you had flames is when you turn the switch off, it stops the spark but the engine continues to spin and pull fuel from the carburetor through the engine for a few revolutions as it comes to a stop. The fuel goes through the engine and into the exhaust. The red hot spots in the muffler caused it to flame.