fix a more subtle memory leak
add test and refactor