Refactor history into functions