Added garbage handler to the loops

To reduce memory usage