Fix bug for history + clean code