Scheduler locking changes to master

This is new PR with the same diff as PR #247 that has been reviewed and merged.