Solution using generators