Implement free memory function