Modularize

This is a big refactor that pushes all the different parts into their own modules.

It also changes the interface a little bit for the API.