fixed invalid reads detected by valgrind