Added tests, and fixed a bug in quality calculation