fix some possible memleaks + segfaults