Fix bugs in serialization