Add history button and refactor code