Null pointer and infinite loop fix.