rewrite of garbage collect to be recursive