Refactored pagination code for list threads.