Refactored serialization code.