better pagination/count code