Major heap corruption issues corrected and cleaned up some of the code