fixed a segment bug, adds tests