Improved code of chapter 5