fix memory errors reported in #348