working generate function