Some memory leaks fixed.
Tested a little bit only... Feel free to revert