Fix bugs with type conversion