refactor generate method