Making joinPaginated function more efficient