Catch the NULL cases, fix memory leak.