This seems to be the correct way to validate a reference. Why is cast_to failing? Is this the correct way of checking if the object is valid?