serialization code and refactoring