Refactoring segment.py. Changed to using a specialized error type.