Script refactoring