Pagination for Gallery and various bug fixes