memory leaks *partially* fixed