works with valgrind, need to fix