Rewritten to remove istype checks