improved generator function