Added toType method instead of instanceof