Refactored code in script2.ps1