cleaned addToHistory() function