improve serialization logic