Fix leaks and improve algo

Fix bug when totalPages was 0 and add get methode "maxPages"