refactor script code