Make cleanup_history safe