Refactored the  script