fixing valgrind invalid read