fixed segmentation issues + tests