tidied up serialize code.