all tests in, plus some restructuring of the code