Adding logging and clean up potential null pointer.