bugfix: using freed memory; made code generally valgrind clean again