Improve implementation of serialization