Improved tests, and more bug fixes