added a missing free and small optimizations