Fix potential nil pointer dereference