More tests, code cleanup