Added stack overflow logic