bug fixes and lots of tests!