Added some casts and type fixes