introduce toType function from StackOverflow
so first function works
failed again