Refactor segment.py . Segments are now objects rather than just dicts.

This is probably not quite ready (I want to write some tests and change the way segments are used) but everything seems to be working with this as a drop in replacement. I wanted to get your thoughts before I keep going because this is becoming a pretty large refactor.