refactored function.generate