Rewrite the scheduler in an agent-centric way

This seems to perform better than the old one.
`file` is a built-in name in < Python 2.7