Added a check and more info-rich exception on cast