Refactoring the script