Bug fix: was always returning DOM element instead of resolving whether to return Document or Element