reworked save pages to history algorithm