Complete refactorization to support pagination.