More tests and some code cleanup