Rewrote the segment selection logic, it was badly broken