Histroty function refactoring