Fixed all segfaults and leaks.