Segfault and program not finishing fixed