finish serialize function, cleanup a bit and moar cleanup