Add unit tests
Fix a issue causing a segment being negative when it's not