Making code more resilient : adding changes as per sriram's feedback