memory leakage removed and checked with valgrind