improve serialization code, still unfinished.