Shell script code refactored to work in remote environment