added some memory cleanup code and memory debugging code