Fixed memory leak

Uncertain if completely fixed
TODO look into it more