many fixes, optimizations, tests and doc changes