added tests; fixed multiple memory leaks