refactor generating function