refactoring mem_out stuff