Improved efficiency by using generators