Added explicit NULL checks.