Adding tests and some small reorganization.