Memory Errors Fixed

Fixed errors where memory was being used by chance in certain functions.