Scheduler locking

This branch branches off from the regrettably misspelled optimimize_scheduler branch. Please merge only this one or the other, but not both.