fix bug with loop optimization in schedule()