Refactored script