Make type casts safer.
Merge pull request #1831 from monaka/pr-make-type-casts-safer

Make type casts safer