refactored script to use timeout, debugged quoting for variable expansion