lots of fixes, tests added