added expandCounterPath. fixed some resource leaks.