Fixed some memory errors, still segfault but using this as a checkpoint