History code rewrite (#1122)