corrected generate function