Pulled out a lot of the backend stuff into a separate library.