Check for empty document  directly in loop