Improved unit tests and fixed bugs