refactoring for outofmemory problem;