Fixed some bugs in the serialization code, it seems to work now.