Refactored script code