Fixed some bugs causing a segfault