Fix casting run time errors