refactoring, added flag for nested documents