Added tests, various fixes. Updated docs