Remove unsafe code and use as for type conversions.

Some performance is lost by removing unsafe but more is gained by using as for type conversion.