Fixing memory leakages. Handling properly exit when test is running.