finished improvements and fixed segmentation fault