fixed free/gc bug; introduced separate gc function