Fixing memory leaks [ci skip]