Various typecasting etc fixes