Refactor history manager
Improved error handling