null ptr dereference fix and proper recursive scan