Add pagination, some refactoring