Added broad (shallow) set of tests. Fixed some bugs revealed by the tests.