Minor changes to improve memory handling
added gc(). Need to come back to this