fixed segfault and file read bug