Fix for script- non-ending loop