Script refactor