Memory patch

Fixed a massive memory management issue