Refactored shell script