Improve main code for generation