cleanup and refactor of segment module, part 1