core: refactor sched_run

`sched_run()` was cluttered, changed changed without refactoring a lot.

Rebased on #1836.
Can you put something like `DEBUG("scheduler: done\n")` or similar here, too?