A few bug fixes and improved test coverage