Fixed some bugs in serialization code