seems like it might be worth making an isContainer() function (or some other name) somewhere for the increasingly long set of conditionals that is being duplicated in three places here.