memory test done, and some refactoring