Fixing cast exceptions