Pagination gives extra page with no results

I am not sure, but I have a guess that it is because you are using pagination and offset at the same time. Pagination might be calculated for whole set, but you are reducing set size with offset so number of pages becomes overestimated.