Fixes severe memory leaks