Added reason to InvalidValue exception.
Added explicit casts to unsigned int.