Greatly simplified the code of the tutorial.