Fixing issues with ClassCastException