Fixed major memory leak and remove a bug that slowed program down over time