Use instanceof rather than .class == for correct subtyping