fixed a few memory leaks and segfaults