extract generate functions to a generic one