Initial work on modularizing scheduling.

And hopefully making it more readable.