Refactor generate method