Implemented checks for maximum recursion depth to prevent segfaulting on infinite recursion.