serialization and deserialization functions; refactoring