Clean up and append to history