fix double free bug

Addresses #340.