Refactored  script