model: refactor "get next thread" code