fixed some bugs including segmentation fault