Fixed bugs.
Added tests.