improved some handling of memory