Added tests and fixed bugs