A little code cleanup
Code break down. Isolated history and utils.
Moved some logging logic inside history.
Formatting.