Clean history only when appropriate