Refactor esp generate