Refactor table serialization code