Attempt to reduce memory usage by using generator