Check memory allocation

"input_cascade" is dereferenced later on so could crash if this allocation fails

Also declare vars as locally as possible