ported proper serializing code to c++.