fixed all of the mallocs and some segfaults