First working version.

Still kind of slow.