Fixed NULL checking bug