Fix cast and type issues