Refactored mem clearing to its own function