Fixed bugs, code compiles with segmentation fault