Extracting  methods to generate segments from Message

Also minor refactoring in other classes.