Some code cleaning, desperately attempting to fix memory leaks