implement garbage collection (still buggy)