Fixed bugs in scheduling code