Reworked conditional to avoid dereferencing NULL pointer