Rearrangements made, fixed CPP generation, added unit tests.