Refactoring serialization logic into separate module.