Fix casts causing run time errors