factor out isTopLevel