Fixed a bug in the state merge that caused a cast exception in some situations