Using the same function as in the book.