works as I would expect it to, but there are almost without question existing memory leaks here, because I am not freeing all of the pointers I need to