Fixed some of the bugs; however the code still segfaults.