Fix memory leak - thanks to @andreiled