Additional checks added. Lack of them led to script failure is some cases.