Added better stack depth safety check.