refine script code