Added proper freeing of memory, now leaks less than 1mb