refactor scheduling code