Refactor serialization