Greatly simplified the serialization logic