adding error checking on type casting