replace Memory() with two different functions and other small improvements