Refactor scripting