Overengineering, plus tests

I finally got a chance to work on this tonight.