Fixed bugs
Added tests