Fixed 3 segmentation fault errors, added debug function