Tried to fix memory leaks.
Fixed merge