Separate function for determining which tasks to run next.