Added type checks before all casts in the standard library