code refactored to save memory