Fix memeory management issues and make compilable against non c++11
Fix some very stupid memeory leaks