core: refactor sched_run

`sched_run()` was cluttered, changed changed without refactoring a lot.

Rebased on #1836.