Refine script generating