Optimized code to save memory

I deleted all the temporary objects created to save some memory