Added some debug prints when allocating new objects, structured code and removed memory leak.