momory refactoring fixes