replacing free() with SAFE_FREE() where possible