fixing bad_cast exception