added some optimization per the last chapter of the tutorial