segment creation logic refactoring