core: refactor sched_run

`sched_run()` was cluttered. Many individual changes were done without a proper refactoring.

Rebased on #1836.