merged from index_dev.
Fixed memory leaks mostly related to exception-safety issues. Modified docs, added tests.