Added handling for classCastException