personally, i find this double `instanceof` logic confusing. is there anything else we can do here to get the same results?