fix logic a bit - avoid dereferencing NULL pointer