fix leaks and bad counters