Memory leaks fixed.

It still has little leaks, but they seem to be caused by system. Also, it stopped failing.
Few more leaks fixed.