added check that doc != null to avoid exception in one of the test cases