Refactor serialization/deserialization to their own functions