Refactored the script