Made script more fault tolerant.