Added tests. Fixed bugs