Fixes #1560 - Type-safety added to two places to prevent crashes

Merging in fix from release/4.0.2 into develop