Fixing async code. Refactoring
Refactoring generate function