code after exercise 8 with Toan the legend