Add the type check logic to avoid crash