Refactor serialize.