fixing memory leak issues reported by valgrind