added better support for first segment
added last segment removal