fixes memory leak issues related to #39 and #40