Added tests, solved bugs