Added gc() calls after rm() and now using tmp objects to avoid using $ as much as possible