stack overflow detection, optimizations