Add tests and do a bit of restructuring.