refactor for history handling
history almost working