Bug fix in getting next task to run