added check that doc is not null