make end condition script more robust