optimize code, add history