Correct serializing/deserializing

Correct serializing/deserializing and make some minor fixes.