(#8040) Test order of application for contained classes

This also includes some minor refactoring of the contain function.