Memory leak fix/thanks to AndreiLux