Rewrite the scheduler in an agent-centric way

This seems to perform better than the old one.
You probably also want `OSError` here too.