Refactor generate