Some fixes on casting to right type